package app
{
		import app.db.GoRemoteConnection;
		
		import data.CONST;
		
		import flash.events.Event;
		import flash.events.MouseEvent;
		import flash.net.Responder;
		
		import mx.controls.Alert;

	
	/**
	 * @author thomas fricke
	 * @version 1.0.0
	 * @date 2009-06-11
	 * @desc a GoLoginHandler handles the login data a certain user has entered.
	 * 		 if the entered data is equal to the corresponding database entry,
	 * 		 the user gets logged in.
	 **/
	public class GoLoginHandler
	{
		
		public function comp():void {
//			login_txt.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
//			pass_txt.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
//			lgnUser.addEventListener(KeyboardEvent.KEY_DOWN, doLogin);
//			lgnPass.addEventListener(KeyboardEvent.KEY_DOWN, doLogin);
		}
			// http://www.flashfocus.nl/forum/showthread.php?t=33175 <-- thanks a lot, guys!
		public var gateway:GoRemoteConnection;
		private var GATEWAY_URL:String = CONST.GATEWAY_LOC;
		private var __login:String;
		private var __pass:String;
		private var U_ID:uint;
		
		// constructor:
		public function Login():void {
			gateway = new GoRemoteConnection(GATEWAY_URL);
//			submit_btn.addEventListener(MouseEvent.CLICK, clickHandler);
			// goBack_btn.addEventListener(MouseEvent.CLICK, goBackHandler);
//			if(ke.charCode == 13) {
//			}
		}
			
		public function submitForm(user:String, pass:String):void {
			gateway = new GoRemoteConnection(GATEWAY_URL);
			// submit_btn.addEventListener(MouseEvent.CLICK, clickHandler);
			
			__login = user;
			__pass  = pass;
			
			// db:
			var responder:Responder = new Responder(handleDoLogin, faultHandler);
			gateway.connect(GATEWAY_URL);
			gateway.call("GoLoginHandler.doLogin", responder, __login, __pass);
			// gateway.close();
		}
		
//		private function handleDoLogin(re:Object):void {	
//		public var bar:GoLoginBar = new GoLoginBar();
		public function handleDoLogin(re:Boolean):void {
			if(re == true) {
				// U_ID = uint(re);
				CONST.IS_LOGGED_IN = true;
				state = true;
				// CONST.VIS_LOGIN = false;
				// bar.changeBar();
				Alert.show("Login: Success!");
			} else {
				CONST.IS_LOGGED_IN = false;
				// CONST.VIS_LOGIN = true;
				Alert.show("Login failed!");
				trace(re);
			}
		}
			
		private function faultHandler(evt:Event):void {
			trace(evt);
		}
		
		[Bindable] private var logonstate:Boolean;
		public function get state():Boolean {
			return logonstate;
		}
		
		public function set state(value:Boolean):void {
			logonstate = value;
		}
		
		private function goBackHandler(evt:MouseEvent):void {
			//
		}
	}
}